/**
 * @overview 创建走马灯围围墙
 * @author wangxingxing
 * @description 鲸云CgsScrollWall
 * @update  2022.03.23
 */
class GScrollWall {
  constructor() {
    window.GScrollWallArray = []
  }

  //添加单个围墙
  add(paramObj) {
    const { name, parentName, option, labeloption } = paramObj
    const optionModified = {
      positions: option.positions,
      style: {
        diffHeight: option.diffHeight || 500,
        color: option.color || '#3388ff',
        speed: option.speed || 10,
        reverse: option.reverse || false,
      },
      show: option.show || true,
    }
    const scrollWall = new CgsScrollWall(optionModified)
    scrollWall.feature3D.attribute = option.attribute //支持添加自定义属性

    //添加label
    const labelOption = {
      position: labeloption.position || [0, 0],
      name: labeloption.name || '',
      labelfill: labeloption.labelfill || 'rgba(255, 255, 0, 1.0)',
      labeloffset: labeloption.labeloffset || [0, -15],
      labelfont: labeloption.labelfont || '24px 微软雅黑',
      labelstroke: labeloption.labelstroke || 'rgba(0, 0, 0, 0.0)',
      labelstrokewidth: labeloption.labelstrokewidth || 0,
      labelscale: labeloption.labelscale || 1,
      labelBackgroundColor: labeloption.labelBackgroundColor || 'rgba(1, 1, 1, 0.8)',
      showLabelBackground: labeloption.showLabelBackground || false,
      labelBackgroundPadding: labeloption.labelBackgroundPadding || [7, 2],
      labelEyeOffset: labeloption.labelEyeOffset || [0, 0, -10],
    }
    const scrollwallLabel = new CgsLabel(labelOption)

    const scrollWallObject = {
      name: name,
      parentName: parentName,
      scrollWall: scrollWall,
      scrollwallLabel: scrollwallLabel,
    }
    window.map.addFeature(scrollWall)
    window.map.addFeature(scrollwallLabel)
    window.GScrollWallArray.push(scrollWallObject)
  }

  //根据name移除单个围墙
  removeByName(name) {
    window.GScrollWallArray.forEach((item, index) => {
      if (item.name == name) {
        window.map.removeFeature(item.scrollWall)
        window.map.removeFeature(item.scrollwallLabel)
        window.GScrollWallArray.splice(index, 1)
      }
    })
  }

  //根据parentName移除一批围墙
  removeByPatName(parentName) {
    const length = window.GScrollWallArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GScrollWallArray[i].parentName == parentName) {
        window.map.removeFeature(window.GScrollWallArray[i].scrollWall)
        window.map.removeFeature(window.GScrollWallArray[i].scrollwallLabel)
        window.GScrollWallArray.splice(i, 1)
      }
    }
  }

  //移除所有围墙
  removeAll() {
    window.GScrollWallArray.forEach((item) => {
      window.map.removeFeature(item.scrollWall)
      window.map.removeFeature(item.scrollwallLabel)
    })
    window.GScrollWallArray = []
  }

  //根据name控制单个围墙的显隐
  setVisibleByName(name, boolean) {
    window.GScrollWallArray.forEach((item) => {
      if (item.name == name) {
        item.scrollWall.setVisible(boolean)
        item.scrollwallLabel.setVisible(boolean)
      }
    })
  }

  //根据parentName控制一批围墙的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GScrollWallArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.scrollWall.setVisible(boolean)
        item.scrollwallLabel.setVisible(boolean)
      }
    })
  }
}

export default GScrollWall
